package com.tanhua.recommend.listener;

import com.tanhua.domain.mongo.PublishScore;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Component
@RocketMQMessageListener(topic = "tanhua-quanzi-recommend",consumerGroup = "tanhua-quanzi-recommend-group")
public class PublishScoreListener implements RocketMQListener<Map> {

    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public void onMessage(Map msgMap) {
       Long userId = Long.parseLong(msgMap.get("userId").toString()) ;
       Long pid =  Long.parseLong( msgMap.get("pid").toString());
       Integer type = Integer.parseInt(  msgMap.get("type").toString());

        Double score = 0D;
        switch (type){
            case 1:{
                score=1D;
                break;
            }case 2:{
                score=5D;
                break;
            }case 3:{
                score=8D;
                break;
            }case 4:{
                score=10D;
                break;
            }case 5:{
                score=-5D;
                break;
            }case 6:{
                score=-8D;
                break;
            }default:{
                break;
            }

        }
        PublishScore publishScore = new PublishScore();
        publishScore.setUserId(userId);
        publishScore.setPublishId(pid);
        publishScore.setScore(score);
        publishScore.setDate(new Date().getTime());
        publishScore.setId(new ObjectId());

        mongoTemplate.save(publishScore);

        log.debug("PublishScore已经插入成功....");
    }
}
